
Ccay.Module.define("Ccay.example.functionDemo.attachment",function(page,$S){
	page.ready=function(){
			
		$("#attachmentDemo").ccayTab();
		
		var uploadDemoGrid; 
		
		page.openDialog=function(){
			var param={
					//上传类型，附件类为Attachment
					uploadType:"Attachment",
					//单个文件上传回调函数
					callback:demoCallback,
					//所有文件上传结束的回调函数
					completeCallback:completeCallback,
					//用户对象，上传完可以回传此对象
					userObject: {rowId:123456}, 			
					closeDialog:true,
					//需要批ID，附件上传时请指定为true
					needBatchId:true,
					//当前批次ID
					batchId:$S("input[name='attachmentBatchId']").val(),
					//上传参数，此参数可以在服务器端接收。attType由附件处理类处理，标示附件类型，
					//附件类型配置在数据字典的App.Attachment节点下，每种类型配置fileTypes（如xls,xlsx,doc,docsx,zip,rar），maxSize，maxCount三个节点的Value
					query:"attType=AttachmentDemo"
			};
			
			Ccay.Component.openUploadDialog("Attachment Demo",param);
		};
		/**
		 *文件上传回调函数	
		 */
		function demoCallback(file,serverData,batchId,userObject){
			Ccay.UI.showMessage("File uploaded for batch:" + batchId + " ,rowId:" +userObject.rowId ,"success" );
			$S("input[name='attachmentBatchId']").val(batchId);	
			$S("#tempFileList").html($S("#tempFileList").html() +  "<p>{0}-<a href='javascript:void(0)' onclick='Ccay.example.functionDemo.attachment.deleteFile(\"{1}\",null,this)'>Delete</a></p>".formatValue(decodeURI(serverData[0].fileName),serverData[0].deleteUrl));		
		}
		/**
		 * 上传文件成功后，清除当前文件
		 */
		page.deleteFile=function(delUrl,data,obj){
			Ccay.doDelete(delUrl,data,function(){
				$(obj).parent().remove();
			});
		};
		function completeCallback(files, batchId, userObject){
			Ccay.UI.topBox("File upload complete for batchId:" + batchId  + " ,rowId:" +userObject.rowId);
		}
		page.listFile = function(){
			Ccay.doGet("services/ccay/demo/attachment/biz/single/nourl",$S("#bizForm").jsonData(),function(result){
				$S("#fileList").html("");//清空内容
				var list  = "<ul>";
				//Ccay.Core.log(result.attachments);
				for(var i =0; i < result.attachments.length ; i++){
					//Ccay.Core.log("attachment.fileName:" +  result.attachments[i].fileName);
					list += "<li>{0}</li>".formatValue(result.attachments[i].fileName); 
				}
				list +="</ul>";
				
				$S("#fileList").html(list);
			});
		};
		
		page.listFileWithUrl = function(){
			Ccay.doGet("services/ccay/demo/attachment/biz/single/withurl",$S("#bizForm").jsonData(),function(result){
				$S("#fileList").html("");//清空内容
				var list  = "<ul>";
				for(var i =0; i < result.attachments.length ; i++){
					list += "<li><a href='{0}'>{1}</a>-<a href='javascript:void(0)' onclick='Ccay.example.functionDemo.attachment.deleteFile(\"{2}\",null,this);'>delete</a></li>".formatValue(
						result.attachments[i].downloadUrl,result.attachments[i].fileName,result.attachments[i].deleteUrl); 
				}
				list +="</ul>";
				
				$S("#fileList").html(list);
			});
		};
	};
	
});